import numpy as np
import matplotlib.pyplot as plt

from optimizer.vcmtsp.deprecated.data_loader import Data
from utils.tools import load_routes, write_line

np.set_printoptions(suppress=True, linewidth=999)
plt.rc('font', family='Times New Roman', weight='normal', size=18)

cases = ['att48',
         'eil51',
         'berlin52',
         'eil76',
         'eil101',
         'ch130']

date = '2021-10-10'

comp = f'./results/{date}/comparison.txt'
write_line(comp, ['name', 'min', 'min', 'min', 'avg', 'avg', 'avg', 'max', 'max', 'max'], mode='w')
scale = 100

for case in cases:
    for m in range(2, 9):
        # case = 'ch130'
        name = f'{case} m{m}'

        data = Data(name=case, m=m)

        # ga_fitness_1 = np.loadtxt(f'./results/{date}/{case}_m{m}_fitness_GA_1.txt')
        # ga_routes_1 = load_routes(f'./results/{date}/{case}_m{m}_routes_GA_1.txt')
        # min_1 = np.min(ga_fitness_1, axis=0)
        # avg_1 = np.average(ga_fitness_1, axis=0)
        # max_1 = np.max(ga_fitness_1, axis=0)

        ga_fitness_2 = np.loadtxt(f'./results/{date}/{case}_m{m}_fitness_GA_2.txt')
        ga_routes_2 = load_routes(f'./results/{date}/{case}_m{m}_routes_GA_2.txt')
        min_2 = np.min(ga_fitness_2, axis=0)
        avg_2 = np.average(ga_fitness_2, axis=0)
        max_2 = np.max(ga_fitness_2, axis=0)

        abc_fitness = np.loadtxt(f'./results/{date}/{case}_m{m}_fitness_ABC.txt')
        abc_routes = load_routes(f'./results/{date}/{case}_m{m}_routes_ABC.txt')
        min_abc = np.min(abc_fitness, axis=0)
        avg_abc = np.average(abc_fitness, axis=0)
        max_abc = np.max(abc_fitness, axis=0)

        aco_fitness = np.loadtxt(f'./results/{date}/{case}_m{m}_fitness_ACO.txt')
        aco_routes = load_routes(f'./results/{date}/{case}_m{m}_routes_ACO.txt')
        min_aco = np.min(aco_fitness, axis=0)
        avg_aco = np.average(aco_fitness, axis=0)
        max_aco = np.max(aco_fitness, axis=0)

        write_line(comp, [name, min_2[-1], min_abc[-1], min_aco[-1],
                                avg_2[-1], avg_abc[-1], avg_aco[-1],
                                max_2[-1], max_abc[-1], max_aco[-1]])

        plt.close('all')
        plt.clf()
        # plt.plot(range(len(avg_1)*scale), avg_1, label=f'GA-1: {min(avg_1):.0f}', ls='-')
        plt.plot(range(len(avg_2)), avg_2, label=f'GA: {min(avg_2):.0f}', ls='--')
        plt.plot(range(len(avg_abc)), avg_abc, label=f'ABC : {min(avg_abc):.0f}', ls=':')
        # plt.plot(range(len(avg_aco)), avg_aco, label=f'ACO : {min(avg_aco):.0f}', ls='-.')
        plt.xticks([])

        plt.legend()
        plt.title(f'Convergent curves on {case} with {m} salesmen')
        plt.tight_layout()
        plt.savefig(rf'./results/{date}/{case}_m{m}_curves.png', dpi=600)

        # plt.close('all')
        # plt.clf()
        # data.draw(plot=plt, routes=ga_routes_1)
        # plt.title(f'routes of GA-1 on {case} with {m} salesmen : {data.cost(routes=ga_routes_1):.0f}', fontsize=16)
        # plt.savefig(rf'./results/{date}/{case}_m{m}_routes_GA_1.png', dpi=600)

        plt.close('all')
        plt.clf()
        data.draw(plot=plt, routes=ga_routes_2)
        plt.title(f'routes of GA-2 on {case} with {m} salesmen : {data.cost(routes=ga_routes_2):.0f}', fontsize=16)
        plt.savefig(rf'./results/{date}/{case}_m{m}_routes_GA_2.png', dpi=600)

        plt.close('all')
        plt.clf()
        data.draw(plot=plt, routes=abc_routes)
        plt.title(f'routes of ABC on {case} with {m} salesmen : {data.cost(routes=abc_routes):.0f}', fontsize=16)
        plt.savefig(rf'./results/{date}/{case}_m{m}_routes_ABC.png', dpi=600)

        # plt.close('all')
        # plt.clf()
        # data.draw(plot=plt, routes=aco_routes)
        # plt.title(f'routes of ACO on {case} with {m} salesmen : {data.cost(routes=aco_routes):.0f}', fontsize=16)
        # plt.savefig(rf'./results/{date}/{case}_m{m}_routes_ACO.png', dpi=600)
